package com.mao.action.voucher;

import java.math.BigDecimal;
import java.util.Date;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.voucher.JsonVoVoucher;
import com.mao.model.organization.Organization;
import com.mao.model.product.Product;
import com.mao.model.voucher.ProductReturnDeliveryVoucher;
import com.mao.model.voucher.ReturnDeliveryVoucher;
import com.mao.model.voucher.VoucherType;
import com.mao.model.voucher.VoucherVerityType;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.common.ICommonService;
import com.mao.service.organization.IOrganizationService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.voucher.IProductReturnDeliveryVoucherService;
import com.mao.service.voucher.IReturnDeliveryVoucherService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;

public class ReturnDeliveryVoucherAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ReturnDeliveryVoucher rdv;

	private IReturnDeliveryVoucherService returnDeliveryVoucherService;

	private IOrganizationService organizationService;

	private IWareHouseService wareHouseService;

	private IProductService productService;

	private IProductReturnDeliveryVoucherService prdvService;

	private ISearchVoucherService searchVoucherService;

	private IProductStockService productStockService;

	private ICommonService commonService;

	private Organization organization;

	private WareHouse wareHouse;

	private String products;

	private ProductReturnDeliveryVoucher prdv;

	private JsonVoVoucher returnVoucher;

	private String id;

	public String execute() {
		if (id != null) {
			rdv = (ReturnDeliveryVoucher) searchVoucherService.getVoucherById(
					VoucherType.returndelivery.getVoucherClassName(),
					Long.valueOf(id));
		}
		return SUCCESS;
	}

	public String saveReturnDeliveryVoucher() {
		organization = organizationService.getOrganizationById(rdv.getOrg()
				.getId());
		wareHouse = wareHouseService.getWareHouseById(rdv.getWareHouse()
				.getId());
		String voucherNo = commonService.getVoucherNo(SessionHelper
				.getSessionUser().getId(), "XSTD", ReturnDeliveryVoucher.class
				.getName());
		rdv.setStore(SessionHelper.getSessionUser().getStore());
		rdv.setOrg(organization);
		rdv.setWareHouse(wareHouse);
		rdv.setDatetime(new Date());
		rdv.setVoucherNo(voucherNo);
		rdv.setVoucherType(VoucherType.returndelivery);
		rdv.setUser(SessionHelper.getSessionUser());
		returnDeliveryVoucherService.saveReturnDeliveryVoucher(rdv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			prdv = new ProductReturnDeliveryVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			prdv.setProduct(singlePro);
			prdv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			prdv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + prdv.getAmount();
			prdv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(prdv.getSubtotal());
			prdv.setVoucher(rdv);
			prdv.setStore(SessionHelper.getSessionUser().getStore());
			prdv.setVoucherType(VoucherType.returndelivery);
			prdvService.savePrdv(prdv);
		}
		// 再存单据的商品总量
		rdv.setProAmount(proAmount);
		// 再存单据的总价
		rdv.setTotalPrice(totalPrice);
		returnDeliveryVoucherService.saveReturnDeliveryVoucher(rdv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(rdv.getId());
		returnVoucher.setVoucherNo(voucherNo);
		return SUCCESS;
	}

	// 审核单据
	public String veriyVoucher() {
		organization = organizationService.getOrganizationById(rdv.getOrg()
				.getId());
		wareHouse = wareHouseService.getWareHouseById(rdv.getWareHouse()
				.getId());
		ReturnDeliveryVoucher updateRdv = (ReturnDeliveryVoucher) searchVoucherService
				.getVoucherById(
						VoucherType.returndelivery.getVoucherClassName(),
						Long.valueOf(id));
		updateRdv.setTransactionAmount(rdv.getTransactionAmount());
		updateRdv.setReceiveAmount(rdv.getReceiveAmount());
		updateRdv.setNo(rdv.getNo());
		updateRdv.setNote(rdv.getNote());
		updateRdv.setOrg(organization);
		updateRdv.setWareHouse(wareHouse);
		updateRdv.setUpdatetime(new Date());
		updateRdv.setIsverity(VoucherVerityType.verityed);
		updateRdv.setUser(SessionHelper.getSessionUser());
		returnDeliveryVoucherService.saveReturnDeliveryVoucher(updateRdv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			prdv = new ProductReturnDeliveryVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			prdv.setProduct(singlePro);
			prdv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			prdv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + prdv.getAmount();
			prdv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(prdv.getSubtotal());
			prdv.setVoucher(updateRdv);
			prdv.setStore(SessionHelper.getSessionUser().getStore());
			prdv.setVoucherType(VoucherType.returndelivery);
			prdvService.savePrdv(prdv);
		}
		// 再存单据的商品总量
		updateRdv.setProAmount(proAmount);
		// 再存单据的总价
		updateRdv.setTotalPrice(totalPrice);
		returnDeliveryVoucherService.saveReturnDeliveryVoucher(updateRdv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(updateRdv.getId());
		returnVoucher.setVoucherNo(updateRdv.getVoucherNo());
		return SUCCESS;
	}

	public String toAddReturnDeliveryVoucher() {
		return SUCCESS;
	}

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public IWareHouseService getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public WareHouse getWareHouse() {
		return wareHouse;
	}

	public void setWareHouse(WareHouse wareHouse) {
		this.wareHouse = wareHouse;
	}

	public String getProducts() {
		return products;
	}

	public void setProducts(String products) {
		this.products = products;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public ReturnDeliveryVoucher getRdv() {
		return rdv;
	}

	public void setRdv(ReturnDeliveryVoucher rdv) {
		this.rdv = rdv;
	}

	public IReturnDeliveryVoucherService getReturnDeliveryVoucherService() {
		return returnDeliveryVoucherService;
	}

	public void setReturnDeliveryVoucherService(
			IReturnDeliveryVoucherService returnDeliveryVoucherService) {
		this.returnDeliveryVoucherService = returnDeliveryVoucherService;
	}

	public IProductReturnDeliveryVoucherService getPrdvService() {
		return prdvService;
	}

	public void setPrdvService(IProductReturnDeliveryVoucherService prdvService) {
		this.prdvService = prdvService;
	}

	public ProductReturnDeliveryVoucher getPrdv() {
		return prdv;
	}

	public void setPrdv(ProductReturnDeliveryVoucher prdv) {
		this.prdv = prdv;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public JsonVoVoucher getReturnVoucher() {
		return returnVoucher;
	}

	public void setReturnVoucher(JsonVoVoucher returnVoucher) {
		this.returnVoucher = returnVoucher;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(
			ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

}
